package com.apestech.api.xml.esb.modul;

import com.apestech.api.condition.ConFieldM;
import com.apestech.api.condition.ConditionStrategyExec;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.LocalVariable;
import com.apestech.framework.util.StringUtil;
import com.apestech.framework.util.callback.SpiltStrCallback;
import com.apestech.framework.xml.plugin.modul.SysCondition;

import java.util.ArrayList;

public class EsbFormStrategy {

    private ConditionStrategyExec strategyExec;
    private SysCondition sysCondition;
    private SqlSession session;

    public EsbFormStrategy(ConditionStrategyExec strategyExec) throws Exception {
        this.strategyExec=strategyExec;
        session=strategyExec.getSession();
        sysCondition= LocalVariable.getConfig().getSysCondition(session);
    }

    public JSONObject getAllInput() throws Exception {
        JSONObject json = new JSONObject();
        EsbFormConditionField field;
        String theme = session.getTheme();
        JSONArray conf = new JSONArray();
        ArrayList<String> list = strategyExec.getAllInput();
        int len = list.size();
        for (int i = 0; i < len; i++) {
            boolean flag=false;
            String k = list.get(i);
            if(k.equals("ID")){
                k=k+"C_";
            }
            int pos=k.indexOf("#");
            final String[] oid1 = {null};
            final String[] title1 = {null};
            String title=null;
            String oid=null;
            if(pos>=0){
                final String[] finalK = {null};
                StringUtil.execSplitStr(k, "#", new SpiltStrCallback() {
                    @Override
                    public void exec(String id) throws Exception {
                        if(oid1[0]==null){
                            oid1[0]=id;
                        } else if(finalK[0] ==null){
                            finalK[0] =id;
                        } else {
                            title1[0]=id;
                        }
                    }
                });
                oid=oid1[0];
                title=title1[0];
                k=finalK[0];
            }
            JSONObject m = sysCondition.getDimensionField(theme+"."+k);
            JSONObject m1=(JSONObject) strategyExec.getConditionStrategy().getMust_input().get(k);
            if(m1!=null){
                if(m==null){
                    m=new JSONObject(m1);
                } else {
                    m.putAll(m1);
                }
            }
            if(m!=null){
                ConFieldM conFieldM=new ConFieldM(k, session, m);   //tablename要补接口
                field = new EsbFormConditionField("bill_query", "djgc", conFieldM, k, null);
                field.setEsb(true);
                JSONObject cf = field.getField();
                if(oid ==null){
                    cf.put("key", k);
                } else {
                    cf.put("key", oid);
                    cf.put("field", oid);
                    if(title!=null){
                        cf.put("label", title);
                        cf.put("placeholder", "请选择"+title);
                    }
                }
                conf.add(cf);
            }
        }
        json.put("master", conf);
        return json;
    }

}
